package com.zdlee.netty.server6;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;


/**
 * @author zdlee
 * @description: ${DESCRIPTION}
 * @create 2018-11-27 15:42
 */
public class NettyServer {

    private static final int PORT = 8000;

    public static void main(String[] args) {

        NioEventLoopGroup bossGroup=new NioEventLoopGroup();
        NioEventLoopGroup workerGroup=new NioEventLoopGroup();
        final ServerBootstrap serverBootstrap=new ServerBootstrap();
        serverBootstrap.group(bossGroup,workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG,1024)
                .option(ChannelOption.SO_KEEPALIVE,true)
                .option(ChannelOption.TCP_NODELAY,true)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new FirstServerHandler());
                    }
                });
        bind(serverBootstrap,PORT);
    }

    private static void bind(ServerBootstrap serverBootstrap,final int port){
        serverBootstrap.bind(port).addListener(future -> {
           if(future.isSuccess())
           {
               System.out.println("端口[" + port + "]绑定成功!");
           }else
           {
               System.err.println("端口[" + port + "]绑定失败!");
           }
        });

    }


}
